package vlms.controller;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.*;
import vlms.service.VLMSServiceProxy;
import wrapper.CartMovie;

/**
 * Servlet implementation class Balance
 */
public class Balance extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Balance() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String message = null; 
		String destination = "/home.jsp";
		HttpSession session = request.getSession();
		Person oPerson = (Person) session.getAttribute("Person");
		String sPersonId = String.valueOf(oPerson.getPersonid());
		System.out.println("Person id in servlet: " + oPerson.getPersonid());
		
		try
		 {
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 SimpleCustomer oSC = proxy.retrieveSimpleCustomerByPersonId(sPersonId);
			 session.setAttribute("SimpleCustomer", oSC);
			 request.setAttribute("balance", oSC.getBalance());			 
			 destination = "/balance.jsp";
		 }
		 catch(RemoteException re)
		 {
			 destination = "/home.jsp";
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
		 }
		
		
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		String message = null; 
		String destination = "/home.jsp";
		HttpSession session = request.getSession();
		Person oPerson = (Person) session.getAttribute("Person");
		String sPersonId = String.valueOf(oPerson.getPersonid());
		
		try
		 {
		
			float amount = Float.parseFloat(request.getParameter("amount"));
			if(amount < 0) {
				throw new NumberFormatException();
			}
		
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 proxy.addBalanceToAccount(sPersonId, String.valueOf(amount));
			 SimpleCustomer oSC = proxy.retrieveSimpleCustomerByPersonId(sPersonId);
			 System.out.println(oSC.toString());
			 session.setAttribute("SimpleCustomer", oSC);
			 System.out.println(oSC.getBalance());
			 request.setAttribute("balance", oSC.getBalance());		
			 request.setAttribute("message", "Amount Added Successfully");
			 destination = "/balance.jsp";
			 RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
				rd.forward(request, response);
		 }
		catch(NumberFormatException e) {
			request.setAttribute("message", "Invalid Amount Value");
			doGet(request, response);
			 
		}
		 catch(RemoteException re)
		 {
			 destination = "/balance.jsp";
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
			 RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
				rd.forward(request, response);
		 }
		
		
		
		
		
	}

}
